﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Drawing;
using System.Data;
using System.Linq;
using System.Text;
using System.Windows.Forms;
using Xeno.Core;
using ComponentFactory.Krypton.Toolkit;

namespace XenoStudio
{
    public partial class ShadeyNodeAreaControl : UserControl
    {
        public XenoEffectParameter ActivePar {get;set;}
        public ShadeyNodeControl ActiveNode{get;set;}
        public Dictionary<KryptonButton,XenoEffectParameter> NodeLUT = new Dictionary<KryptonButton, XenoEffectParameter>();
        public Image GetImage(string name)
        {
            System.Reflection.Assembly thisExe;
            thisExe = System.Reflection.Assembly.GetExecutingAssembly();
            System.IO.Stream file =
                thisExe.GetManifestResourceStream("XenoStudio.Resource." + name);
            if (file == null)
            {
                return null;
            }
            return Image.FromStream(file);
        }
        public Image _bg;
        public ShadeyNodeAreaControl()
        {
            InitializeComponent();
            _bg = GetImage("ShadeyBGSkin.png");
        }

        protected override void OnPaintBackground(PaintEventArgs e)
        {
        }
        private int MouseX = 0, MouseY = 0;
        protected override void OnMouseMove(MouseEventArgs e)
        {

            if (MouseX != e.X || MouseY != e.Y)
            {
                //base.OnMouseMove(e);
                MouseX = e.X;
                MouseY = e.Y;
                if (ActivePar != null)
                {
                    this.Invalidate();
                }
            }

        }
        protected override void OnPaint(PaintEventArgs e)
        {
            base.OnPaint(e);

            
            var graphics = e.Graphics;
        //    base.OnPaint(e);
            //graphics.FillRectangle(Brushes.Navy, ClientRectangle);
            graphics.DrawImage(_bg, ClientRectangle);
            if (ActivePar != null)

            {

                int sx;
                int sy;
                GetParPos(ActivePar,out sx, out sy);
                graphics.DrawLine(new Pen(Color.FromArgb(255, 255, 255)), new Point((int)sx, (int)sy), new Point(MouseX, MouseY));
           
                //NodeArea.Invalidate();
            }
            foreach (var k in NodeLUT.Keys)
            {
                var ep = NodeLUT[k];
                var to = ep.To;
                if (to != null)
                {

                    int x1, x2, y1, y2;
                    x1 = x2 = y1 = y2 = 0;
                    GetParPos(ep, out x1, out y1);
                    GetParPos(to, out  x2, out y2);
                    graphics.DrawLine(new Pen(Brushes.White), new Point(x1, y1), new Point(x2, y2));
                }

            }
        }

        private void GetParPos(XenoEffectParameter ActivePar,out int sx, out int sy)
        {
            ShadeyNodeControl ActiveNode = null;
            foreach (var a in NodeLUT.Keys)
            {
                if (NodeLUT[a] == ActivePar)
                {
                    ActiveNode = a.Parent as ShadeyNodeControl;
                }
            }
            sx = ActiveNode.Left;
            sy = ActiveNode.Top;
            foreach (var b in NodeLUT.Keys)
            {
                if (NodeLUT[b] == ActivePar)
                {
                    sx += b.Left + b.Width/2;
                    sy += b.Top + b.Height / 2;
                    if (ActiveNode.Node.In.Contains(ActivePar))
                    {
                        sx += b.Width / 2;
                    }
                    else
                    {
                        sx -= b.Width / 2;
                    }
                    break;

                }
            }
        }
    }
}
